﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.IO
{
    /// <summary>
    /// This class provides methods to write one or more complex vectors to a xml file.
    /// </summary>
    [Serializable]
    public class GeneralComplexVectorFileWriter : AbstractXmlFileWriter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="stream">The stream of the file writer.</param>
        /// <param name="encoding">The encoding of the file.</param>
        public GeneralComplexVectorFileWriter(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
            this.writeEngine.WriteStartElement("ComplexVectors");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="filename">The filename of the file to create.</param>
        /// <param name="encoding">The encoding of the file.</param>
        public GeneralComplexVectorFileWriter(String filename, Encoding encoding)
            : base(filename, encoding)
        {
            this.writeEngine.WriteStartElement("ComplexVectors");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralVectorFileWriter"/> class.
        /// </summary>
        /// <param name="textWriter">The text writer of the writer.</param>
        public GeneralComplexVectorFileWriter(TextWriter textWriter)
            : base(textWriter)
        {
            this.writeEngine.WriteStartElement("ComplexVectors");
        }

        /// <summary>
        /// Writes the specified complex vector to the specified file.
        /// </summary>
        /// <param name="vector">The complex vector to write.</param>
        public void Write(GeneralComplexVector vector)
        {
            this.writeEngine.WriteStartElement("ComplexVector");

            for (int i = 0; i < vector.Count; i++)
            {
                this.writeEngine.WriteStartElement("Column");
                this.writeEngine.WriteElementString("RealNumberPart", vector.GetComponent(i).RealNumberPart.ToString());
                this.writeEngine.WriteElementString("ImaginaryNumberPart",
                                                    vector.GetComponent(i).ImaginaryNumberPart.ToString());
                this.writeEngine.WriteEndElement();
            }

            this.writeEngine.WriteEndElement();
        }

        /// <summary>
        /// Closes the writer and saves the output values.
        /// </summary>
        public override void Close()
        {
            this.writeEngine.WriteEndElement();
            base.Close();
        }
    }
}